package com.zscat.mallplus.vo;


import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.ums.entity.UmsMember;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;

/**
 * 用户详情封装
 */
public class MemberDetails implements UserDetails {
    private SysUser sysUser;

    private UmsMember member;

    public MemberDetails(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public MemberDetails(UmsMember member) {
        this.member = member;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"), new SimpleGrantedAuthority("ADMIN"));
    }

    @Override
    public String getPassword() {
        if (null != sysUser) {
            return sysUser.getPassword();
        }
        return member.getPassword();
    }

    @Override
    public String getUsername() {
        if (null != sysUser) {
            return sysUser.getUsername();
        } else {
            return member.getUsername();
        }
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (null != sysUser) {
            return member.getStatus() == 1;
        }
        return member.getStatus() == 1;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    public UmsMember getMember() {
        return member;
    }
}
